﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using TakaGUI.Cores;
using TakaGUI.DrawBoxes.Forms;
using TakaGUI.DrawBoxes;
using EvoApp.DrawBoxes;
using TakaGUI.Services;
using TakaGUI;
using EvoSim;
using EvoApp.Worlds;
using System.Xml;
using EvoSim.Genes;
using EvoSim.NeuralNets.RMP;
using EvoSim.NeuralNets;
using System.IO;
using System.Diagnostics;
using TakaGUI.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace EvoApp.States
{
	public class InitializeState : State
	{
		WorldBox worldBox;
		Simulation simulation;

		public override void Initialize(GameTime gameTime)
		{
			Globals.GameScreen = new TakaGUI.Window();
			Globals.GUIManager.GameScreens.Add(Globals.GameScreen);

			Globals.KeyboardInput.Enabled = true;
			Globals.MouseInput.Enabled = true;
			Globals.GUIManager.Enabled = true;
			Globals.GUIManager.Visible = true;

			FpsMeter fpsMeter = new FpsMeter();
			fpsMeter.Initialize();
			Globals.GameScreen.AddDrawBox(fpsMeter);

			EvoSim.Globals.Random = Globals.Random;
			EvoSim.Globals.GaussianRandom = Globals.GaussianRandom;

			simulation = new Simulation();
			simulation.FileName = "test.dat";

			worldBox = new WorldBox();
			worldBox.Initialize();
			Globals.GameScreen.AddDrawBox(worldBox);
			worldBox.FillWidth();
			worldBox.FillHeight();

			var world = new FoodWorld(30, Globals.Random, Globals.GaussianRandom);
			worldBox.World = world;
			world.Width = worldBox.Width;
			world.Height = worldBox.Height;
			world.CreateRegions(30);

			simulation.World = world;

			for (int i = 0; i < 50; i++)
			{
				var neuralSettings = new RMP_ChromosomeSettings();
				var neuralChromosome = new RMP_Chromosome(neuralSettings);
				neuralChromosome.GlobalSigmoidFunction = new DefaultSigmoid(true);
				var globalSigmoid = new DefaultSigmoid(false);
				((DoubleGene)globalSigmoid.GeneList[0]).Value = 0;
				globalSigmoid.GeneList[0].IsMutable = false;
				((DoubleGene)globalSigmoid.GeneList[1]).Value = 1;
				globalSigmoid.GeneList[1].IsMutable = false;
				neuralChromosome.GlobalOutputSigmoidFunction = globalSigmoid;

				var genomeSettings = new GenomeSettings();
				genomeSettings.Crossover = new SinglePointCrossover();
				var genome = new Genome();
				genome.SetSettings(genomeSettings);
				genome.SetNetChromosome(neuralChromosome);

				Creature creature = new Creature();
				creature.CreatureGenome = genome;
				creature.Randomize();

				creature.Speed = 60 / 60.0;
				creature.RotationSpeed = (Math.PI * 2) / (60);
				creature.Energy.Max = 60 * 60 * 5;
				creature.Energy.SetToMax();
				creature.Radius = 16;

				creature.EntityColor = Color.Red;

				creature.Position = new EntityPosition(Globals.Random.Next(world.Width), Globals.Random.Next(world.Height));

				world.AddEntity(creature);
			}
		}

		public override void Work(GameTime gameTime)
		{
			if (Globals.KeyboardInput.IsClicked(Microsoft.Xna.Framework.Input.Keys.S))
			{
				simulation.Save();
			}

			if (Globals.KeyboardInput.IsClicked(Microsoft.Xna.Framework.Input.Keys.L))
			{
				simulation = Simulation.Load(simulation.FileName);
				worldBox.World = simulation.World;
			}
		}
	}
}
